package com.cn.ServiceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.cn.Domain.CheckItem;
import com.cn.Entity.PageResult;
import com.cn.Mapper.CheckItemMapper;
import com.cn.Service.CheckItemService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemMapper checkItemMapper;
    @Override
    public void add(CheckItem checkItem) {
        checkItemMapper.insert(checkItem);
    }


    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckItem> checkItems = checkItemMapper.selectByCondition(queryString);
        return new PageResult(checkItems.getTotal(),checkItems.getResult());
    }

    @Override
    public void delete(Integer id) throws RuntimeException {
        long count = checkItemMapper.findCountByCheckItemId(id);
        if (count>0){
            throw new RuntimeException("当前项目被引用不能删除");
        }
        checkItemMapper.deleteByPrimaryKey(id);
    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(CheckItem checkItem) {
        checkItemMapper.updateByPrimaryKeySelective(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemMapper.selectAll();
    }


}
